package com.app.babygo;

import com.app.babygo.dbservice.ChildAdapter;
import com.app.babygo.exceptions.InternalDBException;
import com.app.babygo.exceptions.ObjectNotFoundException;
import com.app.babygo.help.MessageHelper;
import com.app.babygo.obj.Child;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class ChildInfo extends Activity {

	private Child SelectedChild;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_child_info);
		loadTaskList();
		createUI();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.child_info, menu);
		return true;
	}
	
	private void loadTaskList() {
		Intent intent = this.getIntent();
		SelectedChild = (Child)intent.getSerializableExtra(ChildChoose.SELECTED_CHILD_KEY);
	}
	
	private void createUI() {
		//post the child info
		EditText childname_box_update = (EditText) findViewById(R.id.child_update_name);
		childname_box_update.setText(SelectedChild.getChildname());
		EditText childage_box_update = (EditText) findViewById(R.id.child_age_update);
		childage_box_update.setText(Integer.toString(SelectedChild.getChildage()));
		EditText childescrip_box_update = (EditText) findViewById(R.id.child_info_update);
		childescrip_box_update.setText(SelectedChild.getChildinfo());
		TextView childgender_update = (TextView) findViewById(R.id.child_gender_update);
		if(SelectedChild.getChildgender()==1){
			childgender_update.setText(R.string.childgender_girl);
		} else {
			childgender_update.setText(R.string.childgender_boy);
		}
	}
	
	//button help action
	public void setGirl(View view){
		RadioButton rd_boy = (RadioButton) findViewById(R.id.radio_boy_update);
		rd_boy.setChecked(false);
		TextView childgender = (TextView) findViewById(R.id.child_gender_update);
		childgender.setText(R.string.childgender_girl);
	}
	
	public void setBoy(View view){
		RadioButton rd_girl = (RadioButton) findViewById(R.id.radio_girl_update);
		rd_girl.setChecked(false);
		TextView childgender = (TextView) findViewById(R.id.child_gender_update);
		childgender.setText(R.string.childgender_boy);
	}

	public void updateChildInfo(View view){
		// get the new info
		EditText childname_box_update = (EditText) findViewById(R.id.child_update_name);
		String new_childname = childname_box_update.getText().toString();
		
		EditText childage_box_update = (EditText) findViewById(R.id.child_age_update);
		int new_childage = Integer.parseInt(childage_box_update.getText().toString());
		
		EditText childescrip_box_update = (EditText) findViewById(R.id.child_info_update);
		String new_child_info = childescrip_box_update.getText().toString();
		
		TextView childgender_update = (TextView) findViewById(R.id.child_gender_update);
		String new_childgender = childgender_update.getText().toString();
		
		//check validation
		int child_gender = -1;
		if(new_childgender.equalsIgnoreCase("Boy")){
			child_gender = 1;
		} else if(new_childgender.equalsIgnoreCase("Girl")){
			child_gender = 2;
		} else {
			MessageHelper.showMsg(this, "Child infomation error","Please choose child gender");
			return;
		} if (new_childname.equalsIgnoreCase("")) {
			MessageHelper.showMsg(this, "Child infomation error","Contain empty field");
			return;
		}
		
		SelectedChild.setChildname(new_childname);
		SelectedChild.setChildage(new_childage);
		SelectedChild.setChildgender(child_gender);
		SelectedChild.setChildinfo(new_child_info);
		ChildAdapter adapter = new ChildAdapter(this);
		try {
			adapter.open();
			adapter.updateChildInfo(SelectedChild);
		} catch (InternalDBException e) {
			MessageHelper.showMsg(this, "Error", e.getMessage());
			Log.e("TestError", e.getMessage());
		} catch (ObjectNotFoundException e) {
			MessageHelper.showMsg(this, "Error", e.getMessage());
			Log.e("Object Not found", e.getMessage());
		}
		finally {
			adapter.close();
		}
		Intent intent = new Intent(this, ChildChoose.class);
		startActivity(intent);
	}
}
